package cn.walket.test.db;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import lombok.Getter;
import lombok.SneakyThrows;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;

/**
 * @Author walket
 * @create 2021/2/2 5:34 下午
 */
@Getter
public class IbfsDataSourceConfig extends DataSourceConfig {

  private final List<String> tables = new ArrayList<>();

  @SneakyThrows
  @Override
  public Connection getConn() {
    DbType dbType = getDbType();
    if (dbType == DbType.H2) {
      String scriptPath = System.getProperty("user.dir") + "/user-server/script";
      final List<String> sqlScripts = getConstructTableScriptFiles(scriptPath);
      EmbeddedDatabase db = new EmbeddedDatabaseBuilder(new H2SQLScriptResourceLoader(tables))
          .setType(EmbeddedDatabaseType.H2)
          .setName(getUrl())
          .addScripts(sqlScripts.toArray(new String[]{}))
          .build();
      test(db);
      return db.getConnection();
    }
    return super.getConn();
  }

  public List<String> getTables() {
    return tables;
  }

  private List<String> getConstructTableScriptFiles(String scriptPath) throws IOException {
    final List<String> createSqlScripts = new ArrayList<>();
    final List<String> alterSqlScripts = new ArrayList<>();
    Files.walkFileTree(Paths.get(scriptPath), new SimpleFileVisitor<Path>() {
      @Override
      public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        String fileName = file.getFileName().toString().toLowerCase();
        if (fileName.contains("create") && fileName.endsWith(".sql")) {
          createSqlScripts.add(file.toAbsolutePath().toString());
        }
        if (fileName.contains("alter") && fileName.endsWith(".sql")) {
          alterSqlScripts.add(file.toAbsolutePath().toString());
        }
        return super.visitFile(file, attrs);
      }
    });
    createSqlScripts.addAll(alterSqlScripts);
    return createSqlScripts;
  }

  private void test(EmbeddedDatabase db) throws SQLException {
    ////test1
    Connection connection = db.getConnection();
//      PreparedStatement preparedStatement1 = connection.prepareStatement("SELECT COUNT(*) FROM T_ACTIVITY");
//      ResultSet resultSet2 = preparedStatement1.executeQuery();
//      resultSet2.next();
//      int anInt = resultSet2.getInt(1);
//      System.out.println(anInt);
//      //WHERE TABLE_SCHEMA = 'PUBLIC'
//      CallableStatement callableStatement = connection.prepareCall("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='T_ACTIVITY'");
//      ResultSet resultSet = callableStatement.executeQuery();
//      while (resultSet.next()) {
//        System.out.print(resultSet.getString(1) + " ");
//        System.out.print(resultSet.getString(2) + " ");
//        System.out.print(resultSet.getString(3) + " ");
//        System.out.print(resultSet.getString(4) + " ");
//        System.out.print(resultSet.getString(5) + " ");
//        System.out.print(resultSet.getString(6) + " ");
//        System.out.print(resultSet.getString(7) + " ");
//        System.out.println(resultSet.getString(8));
//      }
//      connection.close();
////test2
//      Connection connection = db.getConnection();
//      CallableStatement callableStatement = connection
//          .prepareCall("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 't_ibfs_activity'");
//      ResultSet resultSet = callableStatement.executeQuery();
//      while (resultSet.next()) {
//        System.out.print(resultSet.getString(1) + " ");
//        System.out.print(resultSet.getString(2) + " ");
//        System.out.print(resultSet.getString(3) + " ");
//        System.out.print(resultSet.getString(4) + " ");
//        System.out.print(resultSet.getString(5) + " ");
//        System.out.print(resultSet.getString(6) + " ");
//        System.out.print(resultSet.getString(7) + " ");
//        System.out.println(resultSet.getString(8));
//      }
//      connection.close();
  }


}
